﻿<%@ Page Title="" Language="C#" MasterPageFile="~/master/m.master" %>

<script runat="server">

</script>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    <link href="../assets/css/plugins/jsTree/style.min.css" rel="stylesheet" />
    <link href="../assets/css/plugins/iCheck/customsmall.css" rel="stylesheet" />
    <link href="../assets/css/plugins/bootstrap-select/bootstrap-select.min.css" rel="stylesheet" />
    <style>
        .system-box .right-side{width: 100%;}
        .system-box .right-content-box{overflow: hidden;}
        .mainContent { height: 100%;background-color: #ffffff; min-width: 650px; }
        .mainContent .part{height: 100%; display: inline-block; background-color: #ffffff; vertical-align: top; border: 1px solid #dddddd;}
        .mainContent .part .title{border-bottom: 1px solid #dddddd; height: 30px; line-height: 30px; padding-left: 5px;    background-color: #eee;}
        .leftPart { width: 350px; max-width: 500px;overflow: auto;}
        .rightPart{ width: calc(100% - 355px); min-width: 320px;overflow: auto;}


        .leftPart .titleText { height: 30px; line-height: 30px; display: inline-block;  }
        .rightPart .titleText { height: 30px; line-height: 30px; display: inline-block;  }


        .leftPart .searchbox{width: 140px; display: inline-block; float: right;}
        .rightPart .searchbox{width: 180px; display: inline-block; float: right;}


        .btn-filter-users { -ms-border-radius: 0px; border-radius: 0px; }


        #prtList table { table-layout: fixed; }
        #prtList td { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }


        #reportDataList tr.check, .table-select tbody tr.check td:first-child {border-left: 4px solid #7acf89;}
        #reportDataList tr.check td, .table-select tbody tr.check td {background: #dff0d8;color: #222121;}

        .wxBox { position: absolute; right: 2px; top: 2px; display: inline-block; }
        .wxBox .dropdown-toggle { padding-top: 3px !important; padding-bottom: 3px !important; }
        .wxTitle { display: inline-block; position: relative; top: 2px; }
        .wxSelect { display: inline-block; min-width:310px; }

        .sweet-alert h2{ line-height: 25px !important;}
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="main" runat="Server">
    <div class="system-box">
        <div class="right-side">
            <div class="btns-container"></div>
            <div class="right-content-box">
                <%--<div class="header-tips">企业微信用户报表授权</div>--%>
                <div class="mainContent">
                    <div class="wxBox">
                        <div class="wxTitle">企业微信：</div>
                        <div class="wxSelect">
                            <select id="wxWorkSelectList" class="selectpicker" title="选择消息类型" data-style="dropdownColor" data-width="100%">
                            </select>
                        </div>
                    </div>
                    <div class="leftPart part">
                        <div class="title">
                            <div  class="titleText">
                                部门用户列表
                            </div>
                            <div class="searchbox">
                                <div class="input-group">
                                    <input type="text" class="form-control input-sm filter-users" id="searchUser"  placeholder="查找用户">
                                    <span class="input-group-btn">
                                        <button class="btn btn-primary btn-filter-users btn-sm" type="button">查找</button>
                                    </span>
                                </div>
                            </div>
                        </div>
                        <div id="groupdata"></div>
                    </div>
                    <div class="rightPart part">
                        <div class="title">
                            <div  class="titleText">
                                报表列表
                            </div>
                            <div class="searchbox">
                                <div class="input-group">
                                    <input type="text" class="form-control input-sm filter-users" placeholder="查找报表标题">
                                    <span class="input-group-btn">
                                        <button class="btn btn-primary btn-filter-users btn-sm" type="button">查找</button>
                                    </span>
                                </div>
                            </div>
                        </div>
                        <div id="prtList">
                            <table class="footable table table-stripped table-list">
                                <thead>
                                <tr>
                                    <th style="width: 40px;"><input type="checkbox" id="prtListcheckAllPlan" /></th>
                                    <th style="width: 40px;">Id</th>
                                    <th style="width: 140px;">报表标题</th>
                                    <th>报表链接</th>
                                </tr>
                                </thead>
                                <tbody id="reportDataList"></tbody>
                            </table>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="footer" runat="Server">
    <script src="../assets/js/plugins/iCheck/icheck.min.js"></script>
    <script src="../assets/js/plugins/iCheck/icheckAll.js"></script>
    <script src="../assets/js/page/rScroll.js"></script>
    <script src="../assets/js/page/pageControl.js"></script>
    <%--<script src="../assets/js/page/tableControl.js"></script>--%>
    <script src="../assets/js/plugins/jsTree/jstree.min.js"></script>
    <script src="../assets/js/plugins/bootstrap-select/bootstrap-select.min.js"></script>
    <script src="../assets/js/plugins/bootstrap-select/i18n/defaults-zh_CN.min.js"></script>
    

    <script type="text/html" id="tpl-rpt-list">
        {{# for(var i = 0, len = d.length; i < len; i++){ }}
        <tr {{# if(i%2 == 0){ }} class="gradeX footable-even"
            {{# }else{ }} class="gradeX footable-odd" {{# } }}>
            <td><input type="checkbox" class="checkPlan report_{{d[i].Id}}"  data-index="{{i}}" /></td>
            <td class="Id">{{d[i].Id}}</td>
            <td class="Title">{{d[i].Title}}</td>
            <td class="Link" title="{{d[i].Link}}">{{d[i].Link}}</td>
        </tr>
        {{# } }}
    </script>

    <script>

        $(document).ready(function () {
            //绑定下拉框
            var bindWxCorpInfoList = function (controller, action, params, els, size, callback) {
                Y.API2(controller, action, params, function (data) {
                    if (data.Success == false) {
                        swal(data.Message, '', 'error');
                    } else {
                        var html = '';
                        data.map(function (item) {
                            html += '<option value=' + item.CIdentification + '>' + (item.CorpName + '-' + item.AgentName) + '</option>';
                        });
                        els.map(function (item) {
                            $(item).html("");
                            $(item).html(html);
                            $(item).selectpicker({
                                size: size,
                                'title': '请选择'
                            });
                            $(item).selectpicker('refresh');
                        });
                        if (typeof callback === "function") {
                            callback();
                        }
                    }
                });
            }
            //绑定数据到企业微信下拉框
            var loadWxCorpInfoList = function (els, callback) {
                bindWxCorpInfoList('WxCorpInfo', 'GetWxCorpInfoList', {}, els, 6, callback);
            }

            //模块ID
            var ModuleID = Y.getUrlParam("ModuleID");

            //获取按钮
            commonUtil.loadBtnList(ModuleID, function () {
                rScroll();
            });

            //自动高 DOCUMENT高减按钮栏+其它
            //$(".chooseArea .leftArea1").height($(document).height() - (35 + 145));




            //获取部门用户tree列表
            var loadDepartmentList = function (wxWorkId) {
                Y.API2('WxUser', 'GetDepartmentList', { wxConfig: wxWorkId}, function (json) {
                    if (json.Success === false) {
                        swal(json.Message, '', 'error');
                    } else {
                        var newdata = $.map(json, function (obj) {
                            obj.parent = obj.parentid == "0" ? "#" : obj.parentid;
                            obj.state = { opened: (obj.parentid === 1) || obj.id === 1 };
                            obj.li_attr = { id: obj.id, userId: obj.UserId, departmentId: obj.DepartmentId,UserName:obj.name };
                            obj.type = (obj.UserId === "" ? "default" : "child");
                            obj.text = obj.name;
                            return obj;
                        });

                        //数据组列表
                        $('#groupdata').jstree({
                                'core': { data: newdata },
                                "checkbox": { "keep_selected_style": false },
                                "search": {
                                    show_only_matches: true
                                },
                                "types": {
                                    "default": { "icon": "fa fa-folder text-info" },
                                    "child": { "icon": "fa fa-user" }
                                },
                                "plugins": ["checkbox", "types", "search"]
                            }).on("ready.jstree", function () {
                            console.log("ready.初始化完毕");
                            layer.closeAll('dialog');

                        }).on("changed.jstree", function (e, data) {
                            //console.log("id=" + data.node.li_attr.id, "uid=" + data.node.li_attr.userId, "departmentId"+data.node.li_attr.departmentId);
                            if (data.action == "deselect_node") {
                                //取消选择的时候不勾选右边列表
                                $("#reportDataList .checkPlan").iCheck('uncheck');
                                return false;
                            }

                            var userId = data.node.li_attr.userId || "";
                            if (userId !== "") {
                                Y.API2('WxReport', "GetReportListByUserId", { UserId: userId }, function (data) {
                                    if (data.Success == false) {
                                        swal(data.Message, '', 'error');
                                    } else {
                                        //console.log(data);
                                        $("#reportDataList .checkPlan").iCheck('uncheck');
                                        for (var i = 0; i < data.length; i++) {
                                            $("#reportDataList .report_" + data[i].ReportId).iCheck('check');
                                        }
                                    }
                                });
                            }

                            //debugger
                            });

                    }
                    // console.log(data)
                });
            }


            //获取报表列表
            var loadReportList = function (wxWorkId) {
                var conditionTitle = $('.rightPart .filter-users').val().trim().replace(/'/g, '');
                pageControl.init('WxReport', 'GetReportList', {
                    conditionTitle: conditionTitle,
                    needLayer: true,
                    wxConfig: wxWorkId,
                    alwaysCallBack: function () {
                        $('#reportDataList').empty();
                    }
                }, function (result) {
                    var tBody = [];
                    if (!Y.checkNull(result)) {
                        if (result.data.length == 0) {
                            Y.NoData(tBody, 5);
                            return false;
                        }
                        var pl = document.getElementById('tpl-rpt-list').innerHTML;
                        laytpl(pl).render(result.data, function (html) {
                            document.getElementById('reportDataList').innerHTML = html;

                            $(".rightPart .footable").icheckAll({
                                checkObj: "#reportDataList .checkPlan", //表格下面的选择控件对象 选择器
                                checkAllObj: "#prtListcheckAllPlan", //全选 选择器
                                clickoutBoxCheck: false, //点击行 不选中
                                checkboxMargin: "4px 0 0 0 " //上右下左
                            });
                        });
                        rScroll();
                    }
                });
            }


            //过虑
            $(".rightPart .btn-filter-users").on("click", function () {
                loadReportList();
            });


            $(".leftPart .btn-filter-users").on("click", function () {
                var v = $('#searchUser').val();
                $('#groupdata').jstree(true).search(v);
            });


            //选中记录
            $('#reportDataList').on('click', 'tr', function () {
                $('#reportDataList tr').removeClass('check');
                $(this).addClass('check');
            });


            var setMemberReportRelation = function (model, actionName) {
                console.log(model);
                Y.API2('WxUser', 'SetMemberReportRelation', model, function (data) {
                    if (data.Success == false) {
                        swal(data.Message, '', 'error');
                    } else {
                        swal(actionName + "成功", '', 'success');
                        $('.filter-users').val("");
                    }
                });
            };


            //提交
            $(document).on("click", "#btn-save", function () {
                var wxWorkId = $('#wxWorkSelectList').selectpicker('val');

                var userIdList = [];
                var selectedNodeIdArr = $('#groupdata').jstree("get_bottom_selected");
                for (var i = 0; i < selectedNodeIdArr.length; i++) {
                    var id = selectedNodeIdArr[i];
                    var node = $('#groupdata').jstree("get_node", id);
                    if (node.li_attr.userId !== "") {
                        var userId = node.li_attr.userId;
                        var userData = {
                            userId: node.li_attr.userId,
                            userName:node.li_attr.UserName
                        }
                        userIdList.push(userData);
                    }
                }

                if (userIdList.length === 0) {
                    swal("请选择要操作的用户", '', 'error');
                    return false;
                }

                var reportIdList = [];
                $('#reportDataList .checkPlan:checked').parents('tr').each(function (index, element) {
                    var id = $(element).find('.Id').text();
                    reportIdList.push(id);
                });

                var model = {
                    WxConfig: wxWorkId,
                    MemberIdList: userIdList,
                    ReportIdList: reportIdList
                }

                var actionName = "保存";
                if (reportIdList.length === 0) {
                    //swal("请选择要关联的报表", '', 'error');
                    actionName = "删除";
                    swal({
                            title: "您没有选择授权报表\r\n继续操作将删除该用户所有已授权报表",
                            text: "删除后将无法恢复，请谨慎操作！",
                            type: "warning",
                            showCancelButton: true,
                            confirmButtonColor: "#DD6B55",
                            confirmButtonText: "删除",
                            cancelButtonText: "取消",
                            closeOnConfirm: false
                        },
                        function() {
                            setMemberReportRelation(model, actionName);
                        });
                } else {
                    setMemberReportRelation(model, actionName);
                }
            });

            //部门-用户模糊搜索
            $('.leftPart .filter-users').keypress(function (event) {
                if (event.which == 13) {
                    var v = $('#searchUser').val();
                    $('#groupdata').jstree(true).search(v);
                }
            });


            $('#wxWorkSelectList').on('changed.bs.select', function (e) {
                var wxWorkId = $('#wxWorkSelectList').selectpicker('val');
                console.log(wxWorkId);
                //layer.msg('玩命加载中……', { icon: 16, shade: 0.01, time: 0, maxWidth: 200 });
                $('#groupdata').jstree("destroy");

                loadDepartmentList(wxWorkId);
                loadReportList(wxWorkId);
            });

            loadWxCorpInfoList(["#wxWorkSelectList"]);

        })
    </script>
</asp:Content>

